const app=getApp();

import Notify from '@vant/weapp/notify/index';
import areaList from "../../utils/area.js"
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
    teamName:app.teamName,
    show:false,
    areaList,
    province: '',
    city: '',
    area: '',
    code:'',
    realname:'',//姓名
    mobile:'',//手机号
    lasttel:'',//紧急联系人
    idCard:'',//身份证
    tXiaoqu:'',//小区
    lng:'',//经度
    lat:'',//纬度
    tShopname:'',//店铺详细地址
    CenterList:[],
    centreId:0,
    centers: [],
    centerIndex:-1,
    remark:'',
    open:false,
    ucode:'',
    upname:'',
    upxiaoqu:'',
    upcenter:0,
    isScene: false,
    show_centers: false,
    serviceTypes:'1',
    // zitiShopShow:true,
    theme:null
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
    let that=this;
    wx.setNavigationBarTitle({
      title: '申请团长'
    })
    let user = app.functions.getLocalStorage("user");
    //二维码进入的注册页面
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      let arrPara = scene.split("_");
      that.setData({
        teamName:app.teamName,
        theme : app.theme,
        isScene:true,
        serviceTypes:String(user.serviceType),
        ucode:arrPara[1]?arrPara[1]:'',
        upcenter:arrPara[0]?arrPara[0]:''
      })
    } else {
      that.setData({
        teamName:app.teamName,
        theme : app.theme,
        isScene:false,
        serviceTypes:String(user.serviceType),
        ucode:options.code?options.code:'',
        upname:options.upname?options.upname:'',
        upxiaoqu:options.upxiaoqu?options.upxiaoqu:'',
        upcenter:options.upcenter?options.upcenter:''
      })
    }
    if(app.functions.isEmpty(user)){
      app.functions.islogin();
      return false;
    }else{
      // that.getCenter();
      //暂时先去掉，记得加上去
        if (user.isteam == 3) {
          Notify({ type: 'warning',duration: 3000, message: '您已经是'+app.teamName+'，三秒后跳至首页！' });
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/index/index'
            })
         }, 4000);
      }
      that.setData({
        serviceTypes:String(user.serviceType),
        theme : app.theme,
        realname:user.realname,
        mobile:user.mobile,
        tXiaoqu:user.tXiaoqu,
        lasttel:user.lasttel,
        idCard:user.idCard,
        province:user.tProvince,
        city:user.tCity,
        area:user.tArea,
        lng:user.lng,
        lat:user.lat,
        tShopname:user.tShopname
      })
      if(user.isteamDesc.length>0){
        let desc=JSON.parse(user.isteamDesc);
        that.setData({
          remark:desc.remark
        })
      }
    }

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

  handleService(event){
    this.setData({
      // zitiShopShow:(event.detail == 1) ? true:false,
      serviceTypes: event.detail
    });
  },

  rName() {
    var reg = /^([\u4e00-\u9fa5·]{2,16})$/;
    if (reg.test(this.data.realname)) {
      return true;
    } else {
      app.functions.showToast('姓名需要为中文！');
      this.setData({
        realname:""
      })
    }
    
  },
  phone() {
    var reg = /^((\+|00)86)?1((3[\d])|(4[5,6,7,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[\d])|(9[1,8,9]))\d{8}$/;
    if (reg.test(this.data.mobile)) {
      return true;
    } else {
      app.functions.showToast('请输入正确的电话号码！');
      this.setData({
        mobile:""
      })
    }
  },
  tel() {
    var reg = /^((\+|00)86)?1((3[\d])|(4[5,6,7,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[\d])|(9[1,8,9]))\d{8}$/;
    if (reg.test(this.data.lasttel)) {
      return true;
    } else {
      app.functions.showToast('请输入正确的电话号码！');
      this.setData({
        lasttel:""
      })
    }
  },
  IdCar() {
    var reg = /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/;
    if (reg.test(this.data.idCard)) {
      return true;
    } else {
      app.functions.showToast('请输入正确的身份证号码！');
      this.setData({
        idCard:""
      })
    }
  },
  // 输入姓名
  bindRealnameInput(e){
    this.setData({
      realname:e.detail
    })
  },
  //手机号
  bindMobileInput(e){
    this.setData({
      mobile:e.detail
    })
  },
  //紧急联系人
  bindLasttelInput(e){
    this.setData({
      lasttel:e.detail
    })
  },
  //身份证
  bindidCardInput(e){
    this.setData({
      idCard:e.detail
    })
  },
  // 选择地区
  getpopupopen(){
    this.setData({
      show:true
    })
  },
  //选择服务中心
  showCenters(){
      this.setData({
        show_centers: true
      })
  },
  onCancel(){
    this.setData({
      show_centers:false
    })
  },
  onConfirm(e){
    this.setData({
      centreId:this.data.CenterList[e.detail.index].centreId,
      centerIndex: e.detail.index,
      show_centers:false
    })
  },
  areaChange(e){
    this.setData({
      values:e.detail.values
    })
  },
  confirm(e){
    let values=e.detail.values;
      this.setData({
        province:values[0].name,
        city:values[1].name,
        area:values[2].name,
        code:values[2].code,
        show:false
      })
  },
  cancel(){
    this.setData({
      show:false
    })
  },
  // 小区
  getChooseLocation(){
    wx.getSetting({
      success (result) {
        if(result.authSetting['scope.userLocation']==false){
          wx.showModal({
            title: '提示', //提示的标题,
            content: '去授权小程序获取地理位置', //提示的内容,
            showCancel: true, //是否显示取消按钮,
            cancelText: '取消', //取消按钮的文字，默认为取消，最多 4 个字符,
            cancelColor: '#000000', //取消按钮的文字颜色,
            confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
            confirmColor: '#3CC51F', //确定按钮的文字颜色,
            success: res => {
              if(res.confirm){
                wx.openSetting({
                  success (res) {
                  }
                })
              }
            }
          });
        }
      }
    })
    var that=this;
    wx.chooseLocation({
        success: function (res) {
          that.setData({
            lng:res.longitude,
            lat:res.latitude,
            tXiaoqu:res.name
          })
        },
        fail: function () {
          wx.openSetting({

          })
        },
        complete: function () {
        
        }
    })

  },
  // 请输入店铺详细地址
  bindtShopnameInput(e){
    this.setData({
      tShopname:e.detail
    })
  },
  // 备注
  bindRemarkInput(e){
    this.setData({
      remark:e.detail.value
    })
  },
  // 服务中心列表
  getCenter(){
    let that=this;
    let params={};
    let user = app.functions.getLocalStorage("user");
    app.api.CenterList({configId:app.configId},(response)=>{
      let row=response.data;
        let center=[];
        row.forEach((item,index,array)=>{
          center.push(item.name);
        })
        that.setData({
          CenterList:row,
          centers:center
        })
        if(app.functions.getLocalStorage("user")){
          row.forEach((item,index)=>{
            if(item.centreId==user.centreId){
              setTimeout(()=>{
                that.setData({
                  centerIndex:index,
                  centreId:item.centreId
                })
              },1000)
              return;
            }
          })
        }
        if(that.data.upcenter>0){
          row.forEach((item,index,array)=>{
            if(item.centreId==that.data.upcenter){
              setTimeout(()=>{
                that.setData({
                  centerIndex:index,
                  centreId:item.centreId
                })
              },1000)
              return;
            }
          })
        }
    },(error)=>{

    })

    
  },
  blurinput(){
    this.setData({
      open:false
    })
  },
  focusinput(){
    this.setData({
      open:true
    })
  },
  //提交
  tosubmit(){
    let that=this;
    if(that.data.realname==''){
      app.functions.showToast("姓名不能为空！");
      return false;
    }else if(that.data.mobile==''){
      app.functions.showToast("手机号不能为空！");
      return false;
    }else if(that.data.lasttel==''){
      app.functions.showToast("紧急联系人不能为空！");
      return false;
    }else if(that.data.lasttel==that.data.mobile){
      app.functions.showToast("手机号不能与紧急联系的相同！");
      return false;
    }
    else if(that.data.centerIndex==-1){
      app.functions.showToast("请选择服务中心！");
      return false;
    }else if(that.data.province==''){
      app.functions.showToast("请选择地区！");
      return false;
    }else if(that.data.tXiaoqu==''){
      app.functions.showToast("小区不能为空！");
      return false;
    }else if(that.data.tShopname==''){
      app.functions.showToast("店铺详细地址不能为空！");
      return false;
    } else if (that.data.serviceTypes == 1){//仅分销服务类型必须要写合作自提点
        if (!that.data.remark) {
          app.functions.showToast("请输入您合作的自提点名称！");
          return false;
        }
    }
    let desc = {
      remark: that.data.remark
    };
    let user=app.functions.getLocalStorage("user");
    if(app.functions.isEmpty(user)){
      app.functions.islogin();
      return false;
    }
    let params={
      realname:that.data.realname,
      mobile:that.data.mobile,
      lasttel:that.data.lasttel,
      idCard:that.data.idCard,
      tProvince:that.data.province,
      tCity:that.data.city,
      tArea:that.data.area,
      tXiaoqu:that.data.tXiaoqu,
      tShopname:that.data.tShopname,
      isteamDesc:JSON.stringify(desc),
      invitaCode:that.data.ucode,
      lng: String(that.data.lng),
      lat: String(that.data.lat),
      tZuobiao: String(that.data.lat) + "," + String(that.data.lng),
      centreId: that.data.centreId,
      id:user.id,
      serviceType:that.data.serviceTypes
    }
    app.api.shopReg(params,(response)=>{
      if (response.data > 0) {
        app.functions.showToast("提交成功，两秒后转到首页");
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/home/home'
          })
        }, 2000);
      } else {
        app.functions.showToast("提交注册失败");
      }
    },(error)=>{

    })
  }
})